// pages/circuitManagementEdit/circuitManagementEdit.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    freight: "", // 运费
    delivery: "",
    show: false,
    list: [],
    value: [0, 0, 3],
    line_id: "",
    arrive_region_id: ""
  },
  bindChange: function(e) {
    console.log(e, '值')
    let {
      value
    } = e.detail || {}, list = this.data.list || [], title = [];
    list.forEach((v, k) => {
      title.push(list[value[0]]['title'])
      if (Array.isArray(v.children)) {
        let children = v.children
      }
    })
    title = [
      list[value[0]]['title'],
      list[value[0]]['children'][value[1]]['title'],
      list[value[0]]['children'][value[1]]['children'][value[2]]['title']
    ]
    this.setData({
      value: e.detail.value,
      delivery: title.join('-'),
      arrive_region_id:list[value[0]]['children'][value[1]]['children'][value[2]].id
    })
  },
  onchange(e){
    this.setData({
      freight:e.detail
    })
  },
  // 保存
  save() {
    let data = {
      depart_region_id: 2063,
      arrive_region_id: this.data.arrive_region_id,
      rate: this.data.freight,
      tansline_id: this.data.line_id
    }
    wx.$api.editRegionList(data).then(res=>{
      if(res.data.code==200){
        wx.navigateTo({
          url: '/pages/circuitManagement/circuitManagement',
        })
      }else{
        wx.$Toast(res.data.msg || '服务繁忙')
      } 
    })
  },
  // 取消
  cancel() {
    this.setData({
      show: false
    })
  },
  // 确认
  affirm() {
    this.setData({
      show: false
    })
  },
  // 选择送达城市
  clickTo() {
    this.setData({
      show: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let lineList = wx.getStorageSync('line') || [];
    this.setData({
      list: lineList,
      line_id: options.id,
      arrive_region_id: options.arrive,
      freight: options.rate || '',
      delivery: options.name || ''
    })
    lineList.forEach(item=>{
      let name = item.title;
      this.forList(item.children, options.arrive, name)
    })
  },

  forList(lineList, id, name){
    lineList.forEach(item=>{
      if(item.id == id){
        this.setData({
          delivery: name += '-' + item.title
        })
      }else{
        if(item.children && item.children.length){
          this.forList(item.children, id, name += '-' + item.title)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})